package com.sixmac.police.controller;

import com.sixmac.common.base.BaseController;
import com.sixmac.core.constant.Constant;
import com.sixmac.core.plugins.dao.Blade;
import com.sixmac.core.plugins.dao.Db;
import com.sixmac.core.plugins.dao.Md;
import com.sixmac.core.toolbox.Paras;
import com.sixmac.core.toolbox.ajax.AjaxResult;
import com.sixmac.core.toolbox.kit.DateKit;
import com.sixmac.core.toolbox.kit.JsonKit;
import com.sixmac.module.*;
import com.sixmac.platform.service.LandlordService;
import com.sixmac.police.service.AdminRoleService;
import com.sixmac.user.ExcelUtil;
import com.sixmac.user.ExportUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * AdminRoleController
 * 2018-09-26 11:15:35
 * @author Blade
 */
@Controller
@RequestMapping("/admin/landlord")
public class AdminLandlordController extends BaseController {
	private static String CODE = "landlord";
	private static String PREFIX = "t_landlord";
	private static String LIST_SOURCE = "landlord.list";
	private static String BASE_PATH = "/police/";

	@Autowired
	LandlordService service;
	
	@RequestMapping(KEY_MAIN)
	public String index(ModelMap mm) {
		AdminUser user = (AdminUser) getRequest().getSession().getAttribute(Constant.SESSION_ADMIN_USER);
		String areaIds = Db.queryStr("SELECT GROUP_CONCAT(u.areaId) areaId FROM t_admin_user u WHERE FIND_IN_SET( u.id, queryAllChildByOrg ('"+user.getId()+"') )",null);
		String areaIds2 = Db.queryStr("SELECT GROUP_CONCAT(area.areaCode) areaCode from t_region_area area LEFT JOIN t_admin_user u ON u.cityId = area.cityCode WHERE u.id is not null and u.id = "+user.getId(),null);
		String area = null;
		if (areaIds!=null&&areaIds2!=null){
			area = areaIds+","+areaIds2;
		}else if (areaIds!=null&&areaIds2==null){
			area = areaIds;
		}else if (areaIds==null&&areaIds2!=null){
			area =areaIds2;
		}

		String startDate = getParameter("startDate");
		String endDate = getParameter("endDate");
		String name = getParameter("name");
		String mobile = getParameter("mobile");
		String idCard = getParameter("idCard");
		Integer sex = getParameterToInt("sex");
		Paras paras = Paras.create().set("startDate",startDate).set("areaIds",area).set("endDate",endDate).set("name",name).set("mobile",mobile).set("idCard",idCard).set("sex",sex);
		Object grid = paginate(Md.getSql(LIST_SOURCE,paras));
		mm.put("grid",grid);
		return BASE_PATH + "landlord_list.html";
	}


	@RequestMapping(KEY_ADD)
	public String add(ModelMap mm) {
		mm.put("code", CODE);
		return BASE_PATH + "landlord_add.html";
	}

	@RequestMapping(KEY_EDIT + "/{id}")
	public String edit(@PathVariable Integer id, ModelMap mm) {
		Landlord landlord = service.findById(id);
		mm.put("model", JsonKit.toJson(landlord));
		mm.put("id", id);
		mm.put("code", CODE);
		return BASE_PATH + "landlord_edit.html";
	}

	@RequestMapping(KEY_VIEW + "/{id}")
	public String view(@PathVariable Integer id, ModelMap mm) {
		Landlord landlord = service.findById(id);
		mm.put("model", JsonKit.toJson(landlord));
		mm.put("id", id);
		mm.put("code", CODE);
		return BASE_PATH + "landlord_view.html";
	}

	@ResponseBody
	@RequestMapping(KEY_LIST)
	public Object list() {
		Paras paras = Paras.create().set("id",getParameterToInt("id"));
		Object grid = paginate(Md.getSql("landlord.roomList",paras));
		return grid;
	}

	@ResponseBody
	@RequestMapping(KEY_SAVE)
	public AjaxResult save() {
		Landlord landlord = mapping(PREFIX, Landlord.class);
		boolean temp = service.save(landlord);
		if (temp) {
			return success(SAVE_SUCCESS_MSG);
		} else {
			return error(SAVE_FAIL_MSG);
		}
	}

	@ResponseBody
	@RequestMapping(KEY_UPDATE)
	public AjaxResult update() {
		Landlord landlord = mapping(PREFIX, Landlord.class);
		boolean temp = service.update(landlord);
		if (temp) {
			return success(UPDATE_SUCCESS_MSG);
		} else {
			return error(UPDATE_FAIL_MSG);
		}
	}

	@ResponseBody
	@RequestMapping(KEY_REMOVE)
	public AjaxResult remove(@RequestParam String ids) {
		int cnt = service.deleteByIds(ids);
		if (cnt > 0) {
			return success(DEL_SUCCESS_MSG);
		} else {
			return error(DEL_FAIL_MSG);
		}
	}

	@RequestMapping("/export")
	public void exportOrderList(String ids,HttpServletRequest request, HttpServletResponse response) throws Exception {
		String excelName = "房东列表" +DateKit.getDays()+ ".xls";

		try {
			AdminUser user = (AdminUser) getRequest().getSession().getAttribute(Constant.SESSION_ADMIN_USER);
			String areaIds = Db.queryStr("SELECT GROUP_CONCAT(u.areaId) areaId FROM t_admin_user u WHERE FIND_IN_SET( u.id, queryAllChildByOrg ('"+user.getId()+"') )",null);
			String areaIds2 = Db.queryStr("SELECT GROUP_CONCAT(area.areaCode) areaCode from t_region_area area LEFT JOIN t_admin_user u ON u.cityId = area.cityCode WHERE u.id is not null and u.id = "+user.getId(),null);
			String area = null;
			if (areaIds!=null&&areaIds2!=null){
				area = areaIds+","+areaIds2;
			}else if (areaIds!=null&&areaIds2==null){
				area = areaIds;
			}else if (areaIds==null&&areaIds2!=null){
				area =areaIds2;
			}

			String startDate = getParameter("startDate");
			String endDate = getParameter("endDate");
			String name = getParameter("name");
			String mobile = getParameter("mobile");
			String idCard = getParameter("idCard");
			Integer sex = getParameterToInt("sex");
			Paras paras = Paras.create().set("startDate",startDate).set("areaIds",area).set("endDate",endDate).set("name",name).set("mobile",mobile).set("idCard",idCard).set("sex",sex);
			String sql = Md.getSql(LIST_SOURCE,paras);
			if (ids.trim().length()>0){
				sql = "SELECT aa.* from("+sql+")aa where aa.id in("+ids+")";
			}
			List<Map> landlords = Db.selectList(sql);
			List<Map<String,Object>> list = new ArrayList<>();
			for (Map map:landlords) {
				Map<String,Object> hashMap = new HashMap<>();
				hashMap.putAll(map);
				list.add(hashMap);
			}
			ExcelUtil.createExcel(ExportUtil.LANDLORD_LIST_TEMPLATE, ExportUtil.LANDLORD_LIST_FIELD, list, ExportUtil.tempFilePath);
			download(request, response, excelName, ExportUtil.tempFilePath);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
